import numpy as np
import cvxpy as cp
c = np.array([[4,8,7,15,12],
              [7,9,17,14,10],
              [6,9,12,8,7],
              [6,7,14,6,10],
              [6,9,12,10,6]])
x = cp.Variable((5,5),integer = True)
obj = cp.Minimize(cp.sum(cp.multiply(c,x)))
con = [0 <= x, x <= 1, cp.sum(x,axis=0,keepdims=True) == 1,
       cp.sum(x,axis=1,keepdims=True) == 1]
prob = cp.Problem(obj,con)
prob.solve(solver = 'GLPK_MI')
print("最优值为：", prob.value)
print("最优解为：", x.value)

L = np.array([48.7,52.0,61.3,72.0,48.7,52.0,64.0])
W = np.array([2000,3000,1000,500,4000,2000,1000])
a = np.array([8,7,9,6,6,4,8])
X = cp.Variable((2,7),integer = True)
obj = cp.Maximize(cp.sum(X * L))
con = [X >= 0, cp.sum(X,axis=0,keepdims=True) <= a.reshape(1,7),
       X * L <= [1020,1020], X * W <= [40000,40000],cp.sum(X[:,4:] * L[4:]) <= 302.7]
prob = cp.Problem(obj,con)
prob.solve(solver = 'GLPK_MI',verbose = True)
print("最优值为：",prob.value)
print("最优解为：",X.value)

x1 = cp.Variable((3,1),integer = True)
y = cp.Variable((3,1),integer = True)
labor = np.array([3,2,6])
cloth = np.array([0.8,1.1,1.5])
profit = np.array([150,220,300])
rent = np.array([200000,150000,100000])
obj = cp.Maximize(profit.reshape((1,3))*x1 - rent.reshape((1,3))*y)
cons = [labor.reshape((1,3))*x1 <= 8200, cloth.reshape((1,3))*x1 <= 8800, x1 >= 0, y >= 0, y <= 1]
prob2 = cp.Problem(obj,cons)
prob2.solve()
print("最优值为：",prob2.value)
print("最优解为：\n",x1.value,y.value)


time = np.array([[3,5,8,4],
                 [6,8,5,4],
                 [2,5,8,5],
                 [9,2,5,2]])
x2 = cp.Variable((4,4),integer = True)
obj2 = cp.Minimize(cp.sum(cp.sum(cp.multiply(time,x2),axis=0,keepdims=True),axis=1,keepdims=True))
con2 = [x2 >= 0, x2 <= 1, cp.sum(x2,axis=0,keepdims=True) == 1,
        cp.sum(x2,axis=1,keepdims=True) == 1]
prob3 = cp.Problem(obj2,con2)
prob3.solve()
print("最优值为：",prob3.value)
print("最优解为：\n",x2.value)